﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AddVoiMotor : Form
    {
        VoiMotor voimotor;

        public AddVoiMotor()
        {
            InitializeComponent();

            this.Text = "Ajouter une voiture to moteur";
            this.buttonAddVoiMotor.Text = "Ajouter";
            /*******/
            DataTable DataVoi = new DataTable();

            DataVoi.Columns.Add("IDVoi");
            DataVoi.Columns.Add("NomVoi");
            DataVoi.Columns.Add("NBPortes");
            DataVoi.Columns.Add("PrixBaseVoi");
            DataVoi.Columns.Add("NumCouleurCaross");
            DataVoi.Columns.Add("NumType");
            DataVoi.Columns.Add("ImageVoi");
            DataVoi.Rows.Add("0", "- Choisir une Voiture -");

            Table couleur = Program.Db.QueryTable("SELECT * FROM VOITURE");
            foreach (Record r in couleur)
                DataVoi.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxVoiture.DataSource = DataVoi;
            this.comboBoxVoiture.ValueMember = "IDVoi";
            this.comboBoxVoiture.DisplayMember = "NomVoi";


            this.comboBoxVoiture.SelectedIndex = 0;
            /*******/
            DataTable DataMotor = new DataTable();

            DataMotor.Columns.Add("IDMotor");
            DataMotor.Columns.Add("ChMotor");
            DataMotor.Columns.Add("NumContenance");
            DataMotor.Columns.Add("NumTypeMoteur");
            DataMotor.Rows.Add("0", "- Choisir un moteur -");

            Table type = Program.Db.QueryTable("SELECT * FROM MOTORISATION");
            foreach (Record r in type)
                DataMotor.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxMotor.DataSource = DataMotor;
            this.comboBoxMotor.ValueMember = "IDMotor";
            this.comboBoxMotor.DisplayMember = "ChMotor";

            this.comboBoxMotor.SelectedIndex = 0;
        }
        public AddVoiMotor(int NumVoi, double NumMotor)
        {
            InitializeComponent();
            
            this.Text = "Modifier une voiture to moteur";
            this.buttonAddVoiMotor.Text = "Modifier";

            DataTable DataVoi = new DataTable();

            DataVoi.Columns.Add("IDVoi");
            DataVoi.Columns.Add("NomVoi");
            DataVoi.Columns.Add("NBPortes");
            DataVoi.Columns.Add("PrixBaseVoi");
            DataVoi.Columns.Add("NumCouleurCaross");
            DataVoi.Columns.Add("NumType");
            DataVoi.Columns.Add("ImageVoi");
            DataVoi.Rows.Add("0", "- Choisir une Voiture -");

            Table couleur = Program.Db.QueryTable("SELECT * FROM VOITURE");
            foreach (Record r in couleur)
                DataVoi.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxVoiture.DataSource = DataVoi;
            this.comboBoxVoiture.ValueMember = "IDVoi";
            this.comboBoxVoiture.DisplayMember = "NomVoi";

            this.comboBoxVoiture.SelectedIndex = 0;

            DataTable DataMotor = new DataTable();

            DataMotor.Columns.Add("IDMotor");
            DataMotor.Columns.Add("ChMotor");
            DataMotor.Columns.Add("NumContenance");
            DataMotor.Columns.Add("NumTypeMoteur");
            DataMotor.Rows.Add("0", "- Choisir un moteur -");

            Table type = Program.Db.QueryTable("SELECT * FROM MOTORISATION");
            foreach (Record r in type)
                DataMotor.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxMotor.DataSource = DataMotor;
            this.comboBoxMotor.ValueMember = "IDMotor";
            this.comboBoxMotor.DisplayMember = "ChMotor";

            Table toto = Program.Db.QueryTable("SELECT * FROM Voi_Motor WHERE NumVoi="+NumVoi+" AND NumMotor ="+NumMotor);
            foreach (Record r in toto)
            {
                this.comboBoxVoiture.SelectedValue = r.Values[0].ToString();
                this.comboBoxMotor.SelectedValue = r.Values[1].ToString();
                voimotor = new VoiMotor(Convert.ToInt32(this.comboBoxVoiture.SelectedValue), Convert.ToDouble(this.comboBoxMotor.SelectedValue));
            }

            this.comboBoxMotor.SelectedIndex = 0;
           
            
        }
        private void buttonAddVoiMotor_Click(object sender, EventArgs e)
        {
            if (comboBoxVoiture.SelectedValue.ToString() != "0" && comboBoxMotor.SelectedValue.ToString() != "0")
            {
                if (this.buttonAddVoiMotor.Text == "Ajouter")
                {
                    voimotor = new VoiMotor(Convert.ToInt32(this.comboBoxVoiture.SelectedValue.ToString()), Convert.ToInt32(this.comboBoxMotor.SelectedValue.ToString()));

                    Thread addThread = new Thread(new ThreadStart(addVoiture_Thread));
                    addThread.Start();
                }
                else if (this.buttonAddVoiMotor.Text == "Modifier")
                {
                    voimotor.set_NumVoi(Convert.ToInt32(this.comboBoxVoiture.SelectedValue.ToString()));
                    voimotor.set_NumMotor(Convert.ToInt32(this.comboBoxMotor.SelectedValue.ToString()));
                    Thread updateThread = new Thread(new ThreadStart(updateType_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        
        }
        private void addVoiture_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            voimotor.new_VoiMotor();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateType_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            voimotor.modify_VoiMotor();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.comboBoxVoiture.Enabled = false;
            this.comboBoxMotor.Enabled = false;
            this.buttonAddVoiMotor.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }
    }
}
